﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using System.Web.Security;
namespace Zebra.Umbraco.Marcos
{
    public partial class SignUp : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            System.Web.Security.Roles.AddUserToRole(CreateUserWizard1.UserName, "BasicUser");
            MembershipUser currentUser = Membership.GetUser(CreateUserWizard1.UserName, true);
            surveyMemberData memberData = new surveyMemberData()
            {
                companyName = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("FullName")).Text.Trim(),
                fullName = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("CompanyName")).Text.Trim(),
                nodeId = int.Parse(currentUser.ProviderUserKey.ToString())
            };
            DataClasses1DataContext db = new DataClasses1DataContext();
            db.surveyMemberDatas.InsertOnSubmit(memberData);
            db.SubmitChanges();

        }

        protected void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)
        {
            Response.Redirect("/my-account.aspx");
        }

        protected void CreateUserWizard1_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                WebQeustion question = QuestionGenerator.GetQuestion();
                ((Label)(this.CreateUserWizardStep1.ContentTemplateContainer.FindControl("lbQuestion"))).Text = question.Question;
                this.ViewState.Add("WebQuestion", question);
            }
        }

        protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {

            string answer = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Passcode")).Text.Trim();
            if (ViewState["WebQuestion"] != null)
            {
                WebQeustion question = (WebQeustion)ViewState["WebQuestion"];
                if (answer != question.Answer)
                {
                    e.Cancel = true;
                    question = QuestionGenerator.GetQuestion();
                    ((Label)(this.CreateUserWizardStep1.ContentTemplateContainer.FindControl("lbQuestion"))).Text = question.Question;
                    this.ViewState["WebQuestion"] = question;
                    ((Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ErrorMessage")).Text = "验证码错误";
                    ((Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ErrorMessage")).Visible = true;
                }
            }
            else
            {
                e.Cancel = true;
                WebQeustion question = QuestionGenerator.GetQuestion();
                ((Label)(this.CreateUserWizardStep1.ContentTemplateContainer.FindControl("lbQuestion"))).Text = question.Question;
                this.ViewState["WebQuestion"] = question;
                ((Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ErrorMessage")).Text = "验证码错误";
                ((Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ErrorMessage")).Visible = true;
            }

        }


    }
}